import asyncio
import os
import sys

from loguru import logger

from com.arcfox.manager.redis_task_manager import RedisTaskManager
from com.arcfox.util.redis_key_manager import YZY_SCHOOL_IMG_LIST_TASK_KEY, YZY_BASIC_UNIVERSITY_SCHOOL_LIST_KEY

sys.path.append(os.path.join(os.path.dirname(__file__), "/root/work/arcfox-crawler"))
from com.arcfox.util.util import add_log
from com.arcfox.school.spider.yzy.yzy_school_img_spider import YZYSchoolImgSpider


class YZYSchoolImgService:
    def __init__(self):
        self.school_img_spider = YZYSchoolImgSpider()
        self.task_manager = RedisTaskManager(YZY_SCHOOL_IMG_LIST_TASK_KEY)
        self.basic_task_manager = RedisTaskManager(YZY_BASIC_UNIVERSITY_SCHOOL_LIST_KEY)
        add_log(self.__class__.__name__ + ".log")

    async def start(self):
        tasks = await self.basic_task_manager.get_all_basic_tasks(True)
        if not tasks:
            logger.info("未获取到基础任务")
        else:
            logger.info("任务生成中...")
            await self.task_manager.add_tasks(tasks)
            await self.school_img_spider.start_crawl()
        logger.info("图片抓取完成")


if __name__ == "__main__":
    asyncio.run(YZYSchoolImgService().start())
